package com.test.deencredit.util_deencredit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.test.deencredit.base_deencredit.BaseApplication_deencredit;
import com.test.deencredit.config_deencredit.CountryConfig_deencredit;

/**
 * 用户信息管理
 * 初始化本地token/mobile等数据
 */
public class UserInfoUtil_deencredit {

    public static final String SP_KEY_TOKEN_deencredit = "token";
    public static final String SP_KEY_MOBILE_deencredit = "phoneNum";
    public static final String SP_KEY_USERID_deencredit = "userid";
    public static final String SP_KEY_ISADSOURCE_deencredit = "isAdSource";
    public static final String SP_KEY_AREACODE_deencredit = "areaCode";

    public static final String SP_KEY_NORMALDIALOG_COUNT_deencredit = "normal_dialog_count";
    public static final String SP_KEY_ACTIVEDIALOG_COUNT_deencredit = "active_dialog_count";

    public static final String LOGIN_TYPE_REGISTER_deencredit = "REGISTER";
    public static final String LOGIN_TYPE_OLD_deencredit = "OLD";

//    /**
//     * 用户是否选择过额度
//     */
//    public static String USER_HAD_SELECTED_AMOUNT_FLAG_deencredit = "NO";

    /**
     * 全局sp
     */
    private final SharedPreferences mSharedPreferences_deencredit;

    /**
     * 登录手机号
     */
    private String mMobile_deencredit;
    /**
     * 登录token
     */
    private String mToken_deencredit;
    /**
     * 登录user_id
     */
    private String mUserId_deencredit;
    /**
     * 是否为渠道用户
     */
    private boolean isAdSource_deencredit;
    /**
     * 登录区号
     */
    private String areaCode_deencredit;

    private int normaldialogCount_deencredit;
    private int activedialogCount_deencredit;

    private static class InnerUserInfoUtil_deencredit {
        private static final UserInfoUtil_deencredit instance_deencredit = new UserInfoUtil_deencredit();
    }

    public static UserInfoUtil_deencredit getInstance_deencredit() {
        return InnerUserInfoUtil_deencredit.instance_deencredit;
    }

    private UserInfoUtil_deencredit() {
        mSharedPreferences_deencredit = BaseApplication_deencredit.getInstant().getSharedPreferences(DecUtil_deencredit.getPackageName_deencredit(), Context.MODE_PRIVATE);
        this.mMobile_deencredit = mSharedPreferences_deencredit.getString(SP_KEY_MOBILE_deencredit, "");
        this.mToken_deencredit = mSharedPreferences_deencredit.getString(SP_KEY_TOKEN_deencredit, "");
        this.mUserId_deencredit = mSharedPreferences_deencredit.getString(SP_KEY_USERID_deencredit, "");
        this.isAdSource_deencredit = mSharedPreferences_deencredit.getBoolean(SP_KEY_ISADSOURCE_deencredit,false);
        this.areaCode_deencredit = mSharedPreferences_deencredit.getString(SP_KEY_AREACODE_deencredit,"");
        normaldialogCount_deencredit = mSharedPreferences_deencredit.getInt(SP_KEY_NORMALDIALOG_COUNT_deencredit,0);
        activedialogCount_deencredit = mSharedPreferences_deencredit.getInt(SP_KEY_ACTIVEDIALOG_COUNT_deencredit,0);
    }

    public void init_deencredit(Context context) {}

    //判断是否已经登录
    public boolean didLogin_deencredit() {
        return !TextUtils.isEmpty(this.mToken_deencredit) && CountryConfig_deencredit.getInstance_deencredit().getAreaCode_deencredit().equalsIgnoreCase(areaCode_deencredit);
    }

    public String getMobile_deencredit() {
        if (TextUtils.isEmpty(mMobile_deencredit)) mMobile_deencredit ="";
        return mMobile_deencredit;
    }

    public String getEncryptMobile_deencredit(){
        if (TextUtils.isEmpty(mMobile_deencredit)) mMobile_deencredit ="";
        int length = mMobile_deencredit.length();
        return "***"+ mMobile_deencredit.substring(length-4,length);
    }

    @SuppressLint("ApplySharedPref")
    public void setMobile_deencredit(String mobile) {
        this.mMobile_deencredit = mobile;
        mSharedPreferences_deencredit.edit().putString(SP_KEY_MOBILE_deencredit, this.mMobile_deencredit).commit();
    }

    public String getToken_deencredit() {
        if (TextUtils.isEmpty(mToken_deencredit)) mToken_deencredit = "";
        return mToken_deencredit;
    }

    public String getUserId_deencredit() {
        if (TextUtils.isEmpty(mUserId_deencredit)) mUserId_deencredit = "";
        return mUserId_deencredit;
    }

    @SuppressLint("ApplySharedPref")
    public void setToken_deencredit(String token) {
        this.mToken_deencredit = token;
        mSharedPreferences_deencredit.edit().putString(SP_KEY_TOKEN_deencredit, this.mToken_deencredit).commit();
    }

    @SuppressLint("ApplySharedPref")
    public void setUserId_deencredit(String userId) {
        this.mUserId_deencredit = userId;
        mSharedPreferences_deencredit.edit().putString(SP_KEY_USERID_deencredit, this.mUserId_deencredit).commit();
    }

    public void setAreaCode_deencredit(String areaCode_deencredit) {
        this.areaCode_deencredit = areaCode_deencredit;
        mSharedPreferences_deencredit.edit().putString(SP_KEY_AREACODE_deencredit, this.areaCode_deencredit).apply();
    }

    public boolean getIsAdSource_deencredit(){
        //tag2024
        return true;
//        return isAdSource;
    }

    @SuppressLint("ApplySharedPref")
    public void setIsAdSource_deencredit(boolean isAdSource){
        this.isAdSource_deencredit = isAdSource;
        mSharedPreferences_deencredit.edit().putBoolean(SP_KEY_ISADSOURCE_deencredit,isAdSource).commit();
    }

    public int getNormaldialogCount_deencredit() {
        return normaldialogCount_deencredit;
    }

    public int getActivedialogCount_deencredit() {
        return activedialogCount_deencredit;
    }

    @SuppressLint("ApplySharedPref")
    public void addNormaldialogCount_deencredit(){
        normaldialogCount_deencredit++;
        mSharedPreferences_deencredit.edit().putInt(SP_KEY_NORMALDIALOG_COUNT_deencredit, normaldialogCount_deencredit).commit();
    }

    @SuppressLint("ApplySharedPref")
    public void addActivedialogCount_deencredit(){
        activedialogCount_deencredit++;
        mSharedPreferences_deencredit.edit().putInt(SP_KEY_ACTIVEDIALOG_COUNT_deencredit, activedialogCount_deencredit).commit();
    }

    public void clear_deencredit(){
        setMobile_deencredit("");
        setUserId_deencredit("");
        setToken_deencredit("");
        setAreaCode_deencredit("");
    }

}
